package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.Result;
import com.itheima.mapper.SpecMapper;
import com.itheima.pojo.Spec;
import com.itheima.service.SpecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
@Slf4j
@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec> implements SpecService {
    @Autowired
    private SpecMapper specMapper;

    @Override
    public Result findAll() {
        List<Spec> specList = this.list();
        return new Result(true, null, specList);
    }

    @Override
    public Result addSpec(Spec spec) {
        boolean isSuccess = this.save(spec);

        if (isSuccess) {
            return new Result(isSuccess, "保存成功", null);
        }
        return new Result(isSuccess, "保存失败", null);

    }

    @Override
    public Result findByPage(@PathVariable("page") Integer currentPage, @PathVariable("size") Integer pageSize) {
        Page<Spec> page1 = new Page<>(currentPage, pageSize);
        this.page(page1);
        List<Spec> specList = page1.getRecords();
        return new Result(true, "查询成功", specList);
    }


    @Override
    public Result findById(Integer id) {
        LambdaQueryWrapper<Spec> wrapper = Wrappers.lambdaQuery(Spec.class);
        wrapper.eq(Spec::getId, id);
        Spec spec = this.getOne(wrapper);
        return new Result(true, "查询成功", spec);
    }

    @Override
    public Result updateById(Integer id, Spec spec) {
        boolean isSuccess = this.updateById(spec);
        if (isSuccess) {
            return new Result(true, "更新成功", null);
        }
        return new Result(false, null, null);
    }

    @Override
    public Result deleteById(Integer id) {
        boolean isSuccess = this.removeById(id);
        if (isSuccess) {
            return new Result(true, "删除成功", null);
        }
        return new Result(false, null, null);
    }

    }

